package com.zhxh.nzpos;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.zhxh.nzpos.login.LoginActivity;
import com.zhxh.nzpos.login.MainActivity;
import com.zhxh.nzpos.serialport.ReadSerialPort;
import com.zhxh.nzpos.utils.HexUtils;

/**
 * 应用的入口
 * 
 * @author wangzt
 * 
 */
public class EntryActivity extends ReadSerialPort {

	String cardId = null;
	public static final String PREFS_NAME = "PreferencesFile";
	public static final String CARD_ID = "cardId";

	public static final String USER = "user";
	public static final String INFORMATION = "dianzhuinfo";

	public static final String PASSWORD = "password";

	public static final String ADMIN = "admin";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_entry);
		Button shuakaBtn = (Button) findViewById(R.id.entry_shuaka_btn);

		 Intent intent = new Intent();
		 intent.setClass(getApplicationContext(),
		 LoginActivity.class);
		 intent.putExtra(Constants.OPERATOR_CARD,
		 TestData.OPERATOR_CARD);
		 intent.putExtra(Constants.OPERATOR_PASSWORD,
		 TestData.OPERATOR_PASSWORD);
		 startActivity(intent);
		 finish();
		// TODO 真正通过刷卡进入登录界面，将卡号和密码带过去，如果没有卡号和密码则不能进入登录界面
		// 或者需要将卡号密码缓存到preference
		shuakaBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// if (cardId != null) {
				// PersistenceUtil.saveOperatorCard(TestData.OPERATOR_CARD,
				// TestData.OPERATOR_PASSWORD, getApplicationContext());
				// }
				 Intent intent = new Intent();
				 intent.setClass(getApplicationContext(),
				 LoginActivity.class);
				 intent.putExtra(Constants.OPERATOR_CARD,
				 TestData.OPERATOR_CARD);
				 intent.putExtra(Constants.OPERATOR_PASSWORD,
				 TestData.OPERATOR_PASSWORD);
				 startActivity(intent);
				// finish();

				Toast.makeText(EntryActivity.this, "请刷卡进入系统",
						Toast.LENGTH_SHORT).show();
			}
		});

	}

	@Override
	protected void onDataReceived(byte[] buffer, int size) {
		synchronized (mByteReceivedBackSemaphore) {
			for (int i = 0; i < size; i++) {
				mReciveData[j++] = buffer[i];
			}
			if (j > 12) {
				if (mReciveData[3] == 9) {
					cardId = String.format("%02x%02x%02x%02x", mReciveData[9],
							mReciveData[10], mReciveData[11], mReciveData[12]);
					Log.i("card", "=====" + cardId);
					// TODO 需要将卡号密码缓存到preference，通过刷卡进入登录界面，将卡号和密码带过去
					if (cardId != null) {
					/*	Intent intent = new Intent();
						intent.setClass(getApplicationContext(),
								LoginActivity.class);
						intent.putExtra(Constants.OPERATOR_CARD, cardId);
						intent.putExtra(Constants.OPERATOR_PASSWORD,
								TestData.OPERATOR_PASSWORD);
						intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
						startActivity(intent);*/
						// finish();
					}
				}
				j = 0;
			}
		}
	}


	@Override
	protected void notifyActivity() {
		synchronized (mByteReceivedBackSemaphore) {
			setPreferences(CARD_ID, card);

			System.out.println("amdin============================="+admin);
			System.out.println("password============================="+password);
			if (admin.endsWith("10af") || admin.endsWith("10AF")) {
				setPreferences(USER, "admin");
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(),MainActivity.class);
				intent.putExtra(Constants.OPERATOR_CARD, card);
				intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				return;
			}

			setPreferences(USER, "dianzhu");
			Intent intent = new Intent();
			intent.setClass(getApplicationContext(), LoginActivity.class);
			intent.putExtra(Constants.OPERATOR_CARD, card);
			intent.putExtra(Constants.OPERATOR_PASSWORD,
					HexUtils.removeZero(password));
			intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			startActivity(intent);

		}
	}

	private void setPreferences(String key, String mPreferences) {
		// set preference
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(key, mPreferences);
		editor.commit();
	}
}
